package com.itheima.servlet;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/addStuudentServlet")
public class addStuudentServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取表单数据
        String username = request.getParameter("username");
        String age = request.getParameter("age");
        String score = request.getParameter("score");
        //创建学生对象
        Student stu = new Student();
       stu.setName(username);
       stu.setAge(Integer.parseInt(age));
       stu.setScore(Integer.parseInt(score));
        //对象数据保存到本地d:\\stu.txt
        BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\add-stu.txt",true));
        bw.write(username+","+age+","+score);
        bw.newLine();
        bw.close();
        //定时刷新响应给浏览器
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("添加成功两秒后跳转到首页");
        response.setHeader("Refresh","2;URL=/stu/");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
